package com.youmingxing.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.MainActivity;
import com.youmingxing.activity.MyNewsActivity;
import com.youmingxing.activity.WorksCommitGuideActivity;
import com.youmingxing.data.GlobalDataManager;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageCenter;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageTable;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageWithInt;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import info.hoang8f.android.segmented.SegmentedGroup;


/**
 * 广场页面
 *
 * @author evilyin(ChenZhixi)
 * @since 15/9/4
 */
public class ContentFragment extends Fragment {

    int pagePos = 1;
    private View view;
    private List<Fragment> fragments = new ArrayList<>();
    private View.OnClickListener listener;

    View myNewsBtn;

    int notReadCount;//未读消息数量
    TextView badgeTv;

    public ContentFragment() {
        // Required empty public constructor
    }

    @Override
    public void onResume() {
        super.onResume();
        MessageWithInt msgToSend = new MessageWithInt();
        if (pagePos == 1) {
            msgToSend.setMsgId(MessageTable.RefreshNews1);
        } else {
            msgToSend.setMsgId(MessageTable.RefreshNews2);
        }
        msgToSend.setInt(pagePos);
        MessageCenter.getInstance().sendMessage(msgToSend);
        if (GlobalDataManager.token.equals("")) {
            // 未登录不显示消息
            badgeTv.setVisibility(View.INVISIBLE);
            myNewsBtn.setVisibility(View.INVISIBLE);
        } else {
            myNewsBtn.setVisibility(View.VISIBLE);
            SelfInfoApi.getMyNewsCount(new BaseJsonHandler(getActivity()) {
                @Override
                public void onSuccessWithData(JSONObject response) {
                    notReadCount = response.optJSONObject("data").optInt("count");
                    if (notReadCount == 0) {
                        badgeTv.setVisibility(View.GONE);
                        if (getActivity() != null) {
                            ((MainActivity) getActivity()).setTabBadge(1, false);
                        }
                    } else {
                        badgeTv.setVisibility(View.VISIBLE);
                        if (getActivity() != null) {
                            ((MainActivity) getActivity()).setTabBadge(1, true);
                        }
                    }
                    badgeTv.setText(notReadCount + "");
                }
            });
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            initListener();
            ContentListFragment hotContent = new ContentListFragment();
            Bundle bundle1 = new Bundle();
            bundle1.putInt("type", 1);
            hotContent.setArguments(bundle1);

            ContentListFragment newContent = new ContentListFragment();
            Bundle bundle2 = new Bundle();
            bundle2.putInt("type", 2);
            newContent.setArguments(bundle2);

            fragments.add(hotContent);
            fragments.add(newContent);

            view = inflater.inflate(R.layout.fragment_content, container, false);
            view.findViewById(R.id.btn_right).setOnClickListener(listener);
            view.findViewById(R.id.content_my_message).setOnClickListener(listener);
            badgeTv = (TextView) view.findViewById(R.id.badge);
            myNewsBtn = view.findViewById(R.id.content_my_message);
            final SegmentedGroup contentSegment = (SegmentedGroup) view.findViewById(R.id.segmented2);
            final ViewPager contentViewPager = (ViewPager) view.findViewById(R.id.content_viewpager);

            contentSegment.check(R.id.content_hot);
            contentSegment.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkedid) {
                    switch (checkedid) {
                        case R.id.content_hot:
                            pagePos = 1;
                            contentViewPager.setCurrentItem(0);
                            MessageWithInt msgToSend = new MessageWithInt();
                            msgToSend.setMsgId(MessageTable.RefreshNews1);
                            msgToSend.setInt(1);
                            MessageCenter.getInstance().sendMessage(msgToSend);
                            break;
                        case R.id.content_new:
                            pagePos = 2;
                            contentViewPager.setCurrentItem(1);
                            MessageWithInt msgToSend1 = new MessageWithInt();
                            msgToSend1.setMsgId(MessageTable.RefreshNews2);
                            msgToSend1.setInt(2);
                            MessageCenter.getInstance().sendMessage(msgToSend1);
                            break;
                    }
                }
            });
            contentViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
                @Override
                public Fragment getItem(int position) {
                    return fragments.get(position);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }
            });
            contentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    switch (position) {
                        case 0:
                            contentSegment.check(R.id.content_hot);
                            break;
                        case 1:
                            contentSegment.check(R.id.content_new);
                            break;
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (view != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
    }

    private void initListener() {
        listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                switch (v.getId()) {
                    case R.id.btn_right:
                        showNewWorkDialog();
                        break;
                    case R.id.content_my_message:
                        intent = new Intent(getActivity(), MyNewsActivity.class);
                        String str = badgeTv.getText().toString();
                        if (!str.equals("0")) {
                            intent.putExtra("have_news", true);
                        } else {
                            intent.putExtra("have_news", false);
                        }
                        startActivity(intent);
                        break;
                }
            }
        };
    }


    private void showNewWorkDialog() {
        Intent intent = new Intent(getActivity(), WorksCommitGuideActivity.class);
        startActivity(intent);
    }
}
